package com.duanjc.doc.es.controller;

import cn.hutool.json.JSONObject;
import com.duanjc.doc.es.controller.dto.Result;
import com.duanjc.doc.es.dto.BatchParamDTO;
import com.duanjc.doc.es.dto.SearchParamDTO;
import com.duanjc.doc.es.entity.WikiPageEsContent;
import com.duanjc.doc.es.service.WikiPageContentEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/es")
public class EsController {

    @Autowired
    private WikiPageContentEsService wikiPageContentEsService;

    @PostMapping("/search.do")
    @ResponseBody
    public Result<List<WikiPageEsContent>> searchPage(@RequestBody SearchParamDTO searchParamDTO) {
        return wikiPageContentEsService.searchWikiPageContent(searchParamDTO);
    }

    @PostMapping("/update.do")
    @ResponseBody
    public String updatePage(@RequestBody WikiPageEsContent w){
        wikiPageContentEsService.updateWikiPageContent(w);
        return "success";
    }

    @PostMapping("/updateBatch.do")
    @ResponseBody
    public String updatePageBatch(@RequestBody BatchParamDTO batchParamDTO){
        List<WikiPageEsContent> list = batchParamDTO.getList().toList(WikiPageEsContent.class);
        wikiPageContentEsService.updatePageBatch(list);
        return "success";
    }

    @PostMapping("/deleteById.do")
    @ResponseBody
    public String deleteById(@RequestBody JSONObject jsonObject){
        String id = jsonObject.get("pageId",String.class);
        wikiPageContentEsService.deleteById(id);
        return "success";
    }
}
